package cn.xutongxue.gtms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.xutongxue.gtms.base.Errors;
import cn.xutongxue.gtms.base.Result;
import cn.xutongxue.gtms.entity.File;
import cn.xutongxue.gtms.service.impl.FileUploadServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author XuTongXue
 * @version 1.0
 * @date 2022/12/30 17:24
 * @Description
 */

@Slf4j
@RestController
@RequestMapping("/upload")
@Api(tags = "文件上传Controller")
@CrossOrigin
public class UploadController {

    @Autowired
    private FileUploadServiceImpl fileUploadService;

    @PostMapping("/file")
    @ApiOperation(value = "文件上传")
    @SaCheckLogin
    public Result uploadFile(@ApiParam(name = "上传的文件", required = true) @RequestParam MultipartFile file) {
        File uploadFile = fileUploadService.uploadFile(file);
        if (uploadFile == null) {
            return Result.fail(Errors.OPERATION_ERROR);
        }
        return Result.success(uploadFile);
    }

    @PostMapping("/avatar")
    @ApiOperation(value = "头像上传")
    @SaCheckLogin
    public Result uploadAvatar(@ApiParam(name = "上传的文件", required = true) @RequestParam MultipartFile file) {
        File uploadFile = fileUploadService.uploadAvatar(file);
        if (uploadFile == null) {
            return Result.fail(Errors.OPERATION_ERROR);
        }
        return Result.success(uploadFile);
    }

}
